package com.liuyjy.utils.utils;

import toolgood.words.StringSearch;
import toolgood.words.WordsSearch;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author liuyjy
 * @Date 2023/3/1 14:03
 * @Description: 敏感词处理
 **/
public class SensitiveWordUtil {


    public static void main(String[] args) {
        String text = "妈妈";

        System.out.println("是否包含敏感词==" + SensitiveWordUtil.containsAny(text));
        System.out.println("包含的所有敏感词==" + SensitiveWordUtil.findAll(text));
        System.out.println("包含的第一个敏感词==" + SensitiveWordUtil.findFirst(text));
        System.out.println("敏感词过滤==" + SensitiveWordUtil.replace(text));
    }


    /**
     * 根据文件路径获取到list集合
     *
     * @param br 文件
     * @return List
     */
    private static List<String> readTxt(BufferedReader br) {
        List<String> list = new ArrayList<String>();
        try {
            //BufferedReader br = new BufferedReader(new FileReader(filePath));
            String line = null;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 判断是否存在敏感词
     *
     * @param txt 内容
     * @return
     */
    private static StringSearch getWords(String txt) {
//        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
//        URL url = classLoader.getResource("sensiWords.txt");
//        String path = url.getPath();


        InputStream u1 = WordsSearch.class.getClassLoader().getResourceAsStream("stastic/sensiWords.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(u1));
        List<String> list = readTxt(br);
        StringSearch iwords = new StringSearch();
        iwords.SetKeywords(list);
        return iwords;
    }

    /**
     * 判断是否存在敏感词
     *
     * @param txt 内容
     * @return
     */
    public static Boolean containsAny(String txt) {
        StringSearch iwords = getWords(txt);
        return iwords.ContainsAny(txt);
    }

    /**
     * 获取包含敏感词
     *
     * @param txt 内容
     * @return List
     */
    public static List<String> findAll(String txt) {
        StringSearch iwords = getWords(txt);
        return iwords.FindAll(txt);
    }


    /**
     * 获取第一个敏感词
     *
     * @param txt 内容
     * @return String
     */
    public static String findFirst(String txt) {
        StringSearch iwords = getWords(txt);
        return iwords.FindFirst(txt);
    }

    /**
     * 敏感词过滤用*替代
     *
     * @param txt 内容
     * @return String
     */
    public static String replace(String txt) {
        return replace(txt, '*');
    }

    /**
     * 敏感词过滤用*替代
     *
     * @param txt     内容
     * @param replace 敏感词替代字符 '*'
     * @return String
     */
    public static String replace(String txt, char replace) {
        StringSearch iwords = getWords(txt);
        return iwords.Replace(txt, replace);
    }


}
